<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="tuichu">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="collapse ? '64px' : '200px'">
        <div class="Collapse" @click="changeCollapse">|||</div>
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
          :unique-opened="true"
          :collapse="collapse"
          :collapse-transition="false"
          :router="true"
          :default-active="path"
        >
          <!-- :default-active="path"当前激活菜单的index-->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item
              :index="'/' + itemChild.path"
              v-for="itemChild in item.children"
              :key="itemChild.id"
              ><i class="el-icon-menu"></i
              >{{ itemChild.authName }}</el-menu-item
            >
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      menuList: [],
      iconObj: {
        125: 'el-icon-user-solid',
        103: 'el-icon-info',
        101: 'el-icon-s-goods',
        102: 'el-icon-s-order',
        145: 'el-icon-s-data'
      },
      collapse: false,
      path: ''
    }
  },
  methods: {
    // 退出函数,清除本地token,并跳转登录页面
    tuichu() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取菜单数据函数
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    // 单击后改变菜单折叠状态
    changeCollapse() {
      this.collapse = !this.collapse
    }
  },
  created() {
    // 创建后执行获取菜单数据函数
    this.getMenuList()
    console.log(111)
  },
  updated() {
    // 页面刷新和输入URL正确显示高亮
    if (this.path === this.$route.path) return
    this.path = this.$route.path
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100vh;
  .el-header {
    background-color: dimgray;
    color: white;
    display: flex;
    justify-content: space-between;
    align-items: center;
    div {
      height: 100%;
      display: flex;
      align-items: center;
      img {
        height: 100%;
      }
      span {
        font-size: 24px;
        margin-left: 15px;
      }
    }
  }
  .el-aside {
    background-color: rgb(84, 92, 100);
    .el-menu {
      border-right: 0;
    }
    .Collapse {
      color: white;
      letter-spacing: 4px;
      text-align: center;
      height: 20px;
      line-height: 20px;
      cursor: pointer;
    }
  }
}

.el-main {
  background-color: rgb(250, 249, 246);
}
</style>
